package com.lrw.springboot_mybatis.backend.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrw.springboot_mybatis.backend.entity.User;
import com.lrw.springboot_mybatis.backend.mapper.UserMapper;
import com.lrw.springboot_mybatis.backend.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.R;
import static javafx.scene.input.KeyCode.V;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-05-07
 */
@RestController
@RequestMapping("/backend/user")
public class UserController {


    @Autowired
    private IUserService userService;

    @Autowired
    private UserMapper userMapper;


    @RequestMapping("list")
    public List<User> userList(User user) {
        user.setName("%Jack%");
        return userService.list();
    }

    @RequestMapping("info")
    public User getUserInfo(Integer id) {
        return userService.getById(id);
    }

    @RequestMapping("add")
    public boolean add(User user) {
        return userService.save(user);
    }

    @RequestMapping("edit")
    public boolean edit(User user) {
        return userService.update(user, null);
    }

    @RequestMapping("delete")
    public boolean delete(Integer id) {
        return userService.removeById(id);
    }

    @RequestMapping("total")
    public int total() {
        return userService.count();
    }

    @RequestMapping("count")
    public int count() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name", "Jack").lt("age", 25);

        return userService.count(wrapper);
    }

    @RequestMapping("chain")
    public boolean chain(User user) {

        // 链式查询 普通
//        QueryChainWrapper<User> query = userService.query();
//        User one = query.eq("name", "zhang").one();
//        UpdateChainWrapper<User> update = userService.update();
//        return update.eq("name", "zhang").remove();

        // 链式查询 lambda式。注意：不支持kotlin
//        LambdaQueryChainWrapper<User> wrapper = userService.lambdaQuery();
//        return wrapper.eq(User::getId, 5).list();
        LambdaUpdateChainWrapper<User> wrapper = userService.lambdaUpdate();
        return wrapper.eq(User::getId, 5).update(user);
    }

    @RequestMapping("lambda")
    public void lambdaTest() {
        List<User> list = userService.list();
        list.forEach(item -> System.out.println(item));
    }

    @RequestMapping("page")
    public IPage<User> pageQuery() {
        Page<User> page = new Page<>(1,20);
        IPage<User> userIPage = userMapper.selectPageVo(page, 20);

        return userIPage;
    }


}
